package com.zhentao.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.entity.product.ProductSpec;
import com.zhentao.service.ProductSpecService;
import com.zhentao.vo.common.Result;
import com.zhentao.vo.common.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value="/admin/product/productSpec")
public class ProductSpecController {

    @Resource
    private ProductSpecService productSpecService ;

    /**
     * 商品规格
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result<Page<ProductSpec>> findByPage(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<ProductSpec> pageInfo = productSpecService.findByPage(page, limit);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 添加商品规格
     * @param productSpec
     * @return
     */
    @PostMapping("save")
    public Result save(@RequestBody ProductSpec productSpec) {
        productSpec.setCreateTime(new Date());
        productSpec.setIsDeleted(0);
        productSpecService.save(productSpec);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 修改商品规格
     * @param productSpec
     * @return
     */
    @PutMapping("updateById")
    public Result updateById(@RequestBody ProductSpec productSpec) {
        productSpec.setUpdateTime(new Date());
        productSpecService.updateById(productSpec);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 删除商品规格
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result removeById(@PathVariable Long id) {
        productSpecService.removeById(id);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 加载商品规格数据
     * @return
     */
    @GetMapping("findAll")
    public Result findAll() {
        List<ProductSpec> list = productSpecService.list(Wrappers.<ProductSpec>lambdaQuery().eq(ProductSpec::getIsDeleted,0));
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }
}